package com.xmim.fulcrum;

import com.xmim.core.constants.MessageCode;
import com.xmim.core.constants.MessageConstants;
import com.xmim.core.entity.Message;
import com.xmim.core.entity.Notification;
import com.xmim.core.session.user.UserSession;
import com.xmim.fulcrum.node.DefaultFulcrumUserNode;
import com.xmim.fulcrum.node.FulcrumServiceNode;
import com.xmim.fulcrum.session.FulcrumNodeBind;

import java.util.UUID;

public class MessageBuilder {

    public static FulcrumNodeBind buildFulcrumNodeBind(UserSession session, FulcrumServiceNode serviceNode) {
        DefaultFulcrumUserNode userNode = new DefaultFulcrumUserNode();
        userNode.setSessionId(session.getSessionId());
        userNode.setHost(session.getAttribute(UserSession.ATTR_HOST));
        userNode.setPort(session.getAttribute(UserSession.ATTR_PORT));
        userNode.setValid(session.isValid());
        if (session.getUser() != null) {
            userNode.setUserId(session.getUser().getUserId());
            userNode.setUser(session.getUser());
        }
        return new FulcrumNodeBind(userNode, serviceNode);

    }


    public static Message<FulcrumNodeBind> buildNodeBindMsg(int type, UserSession session, FulcrumServiceNode serviceNode) {
        return Message.<FulcrumNodeBind>builder()
                .sessionId(session.getSessionId())
                .messageId(UUID.randomUUID().toString())
                .messageType(type)
                .body(buildFulcrumNodeBind(session, serviceNode))
                .build();
    }

    public static <T> Notification<T> buildNotification(int notificationType, T data) {
        return new Notification<T>(notificationType, data);

    }

    public static <T> Message<Notification<T>> buildNotificationMsg(int notificationType, T data) {
        return Message.<Notification<T>>builder()
                .messageType(MessageCode.MSG_FULCRUM_NOTIFICATION)
                .messageId(UUID.randomUUID().toString())
                .body(buildNotification(notificationType, data))
                .build();
    }
}
